﻿#region 命名空间

using System;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.GuestBook
{
    /// <summary>
    ///     留言管理
    /// </summary>
    [Access("258001")]
    public partial class Manage : WebFactory.PageBase
    {
        private static readonly GuestBookManage Bll = PolicyInjection.Create<GuestBookManage>();
        private readonly IP2Location _address = new IP2Location();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!(Page.IsPostBack))
            {
                IsBindEmptyData = true;
                Bind(1);

                this.Panel1.Visible = true;
                this.Panel2.Visible = false;
            }
        }

        private void Bind(int PageNo)
        {
            this.PageNavigation1.Visible = true;

            var pp = new PagingParam();
            pp.PageSize = this.PageNavigation1.PageSize;
            pp.PageNo = PageNo;

            this.DB_GuesetBook.DataSource = Bll.BindList(pp);
            this.DB_GuesetBook.DataBind();

            this.PageNavigation1.RecordCount = pp.RecordCount;
        }


        protected string UBB(string content)
        {
            return HTML.Ubb2Html(content);
        }

        protected string Address(object ip)
        {
            this._address.IP = ip.ToString();
            return this._address.IPLocation();
        }

        #region DB_GuesetBook_ItemDataBound

        protected void DB_GuesetBook_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            Button B;

            foreach (DataListItem oDataListItem in this.DB_GuesetBook.Items)
            {
                B = (Button) oDataListItem.FindControl("btnDelete");
                B.Attributes.Add("onClick", "javascript:return confirm('确定要删除吗？')");
            }

            foreach (DataListItem oDataListItem in this.DB_GuesetBook.Items)
            {
                B = (Button) oDataListItem.FindControl("btnReply");
            }
        }

        #endregion

        /// <summary>
        ///     添加回复
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Reply_Click(object sender, EventArgs e)
        {
            Bll.PostReply(_ID, this.txtContent.Text);
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;

            Bind(1);
        }


        protected void PageNavigation1_PageChanging(object src, WebControls.PageChangingEventArgs e)
        {
            Bind(1);
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Bind(1);
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
        }

        protected void DB_GuesetBook_ItemCommand(object source, DataListCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "ReplyGB": //进入回复界面
                    Bll.ReplyLoad(this.DB_GuesetBook, this.txtContent, e.CommandArgument.ToString());
                    _ID = e.CommandArgument.ToString();
                    this.PageNavigation1.Visible = false;
                    this.Panel1.Visible = true;
                    this.Panel2.Visible = true;

                    break;
                case "DeleteGB": //删除
                    var res = Bll.Delete(e.CommandArgument.ToString());
                    if (res.Errors.Length > 0)
                    {
                        MsgBox.Message(res.Errors, this.UpdatePanel1);
                        return;
                    }

                    this.Panel1.Visible = true;
                    this.Panel2.Visible = false;
                    Bind(1);
                    break;
            }
        }

        #region 留言ID

        /// <summary>
        ///     留言ID
        /// </summary>
        private string _ID
        {
            get
            {
                if (ViewState["GB_ID"] != null)
                {
                    return ViewState["GB_ID"].ToString();
                }
                return "0";
            }
            set { ViewState["GB_ID"] = value; }
        }

        #endregion

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            var IDs = GetSelectedRow.GetIDs(this.DB_GuesetBook);
            var res = Bll.Delete(IDs);
            if (res.Errors.Length > 0)
            {
                MsgBox.Message(res.Errors, this.UpdatePanel1);
                return;
            }


            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
            Bind(1);
        }

        protected void btnDeleteForIP_Click(object sender, EventArgs e)
        {
            var res = Bll.DeleteForIP(this.txtIP.Text.Trim());
            if (res.Errors.Length > 0)
            {
                MsgBox.Message(res.Errors, this.UpdatePanel1);
                return;
            }

            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
            Bind(1);
        }
    }
}